package com.honglang.module.article.service;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.honglang.common.tools.Constants;
import com.honglang.common.tools.Pager;
import com.honglang.module.article.entity.BlogArticleComment;
import com.honglang.module.article.dao.BlogArticleCommentDao;


public class BlogArticleCommentServiceImpl implements BlogArticleCommentService{
	
	private BlogArticleCommentDao blogArticleCommentDao;

	
	
	/**
	 * 
	 * @param blogArticleId
	 * @return
	 */
	public List<BlogArticleComment> getBlogArticleCommentByBlogArticleId(Integer blogArticleId){
		DetachedCriteria dc = DetachedCriteria.forClass(BlogArticleComment.class);
		dc.add(Restrictions.eq("blogArticle.id", blogArticleId));
		return this.getBlogArticleCommentDao().getByDetachedCriteria(dc);
	}
	
	/**
	 * 
	 * @param blogArticleId
	 * @return
	 */
	public List<BlogArticleComment> getBlogArticleCommentByBlogArticleIds(String blogArticleIds){
		List<Integer> integerList = new ArrayList<Integer>();
		for(String id :blogArticleIds.split(Constants.blogArticle_Split)){
			integerList.add(Integer.valueOf(id));
		}
		DetachedCriteria dc = DetachedCriteria.forClass(BlogArticleComment.class);
		dc.add(Restrictions.in("blogArticle.id",integerList));
		return this.getBlogArticleCommentDao().getByDetachedCriteria(dc);
	}
	
	
	public void deleteBlogArticleComment(List<BlogArticleComment> comments){
		this.getBlogArticleCommentDao().deleteAll(comments);
	}
	
	
	/**
	 * get the blogArticleComments by the rootCommentId
	 * @param rootCommentId
	 * @return
	 */
	public List<BlogArticleComment> getBlogArticleCommentByRootCommentId(Integer rootCommentId){
		DetachedCriteria dc = DetachedCriteria.forClass(BlogArticleComment.class);
		dc.add(Restrictions.eq("rootComment.id", rootCommentId))
		  .add(Restrictions.isNotNull("toComment"))
		  .addOrder(Order.asc("commentDate"));
		return this.getBlogArticleCommentDao().getByDetachedCriteria(dc);
	}
	
	
	
	/**
	 * get the  blogArticleComments of one page which comment is the rootComment
	 * @param pageSize
	 * @param pageNo
	 * @return
	 */
	public Pager getBlogArticleRootCommentsPager(int pageSize,int pageNo,Integer blogArticleId){
		int rowCount = this.getBlogArticleRootCommentRowCount(blogArticleId);
		Pager pager = new Pager();
		pager.CreatePager(pageSize, pageNo, Constants.pageIndexCountPerPageInComment, rowCount, this.getBlogArticleCommentDao().getBlogArticleRootCommentsPerPage(pageSize, pageNo, blogArticleId));
		return pager;
	}
	
	
	/**
	 * get the blogArticleRootComment rowCount ;
	 * @param blogArticleId
	 * @return
	 */
	public Integer getBlogArticleRootCommentRowCount(Integer blogArticleId){
		return this.getBlogArticleCommentDao().getBlogArticleRootCommentRowCount(blogArticleId);
	}

	
	
	/**
	 * save the blogArticleComment
	 * @param blogArticleComment
	 * @return
	 */
	public BlogArticleComment saveBlogArticleComment(BlogArticleComment blogArticleComment){
		return this.getBlogArticleCommentDao().merge(blogArticleComment);
	}
	
	
	
	/**
	 * get the blogArticleComment by ID;
	 * @param blogArticleCommentId
	 * @return
	 */
	public BlogArticleComment getBlogArticleCommentById(Integer blogArticleCommentId){
		return this.getBlogArticleCommentDao().getById(blogArticleCommentId);
	}
	


	public BlogArticleCommentDao getBlogArticleCommentDao() {
		return blogArticleCommentDao;
	}


	public void setBlogArticleCommentDao(BlogArticleCommentDao blogArticleCommentDao) {
		this.blogArticleCommentDao = blogArticleCommentDao;
	}


	
}
